const moment = require("moment");
const _ = require("lodash");
module.exports = (env) => {
    /*---------过滤器---------*/
    env.addFilter("formatUrl", (url) => {
        let imageUrl = url.split(",")[0];
        if (imageUrl.indexOf("http://") >= 0 || imageUrl.indexOf("https://") >= 0) {
            return imageUrl;
        } else {
            return `http://${imageUrl}`;
        };
    });
    /*---------格式化json--------*/
    env.addFilter("jsonParse", (str) => {
        try {
            return JSON.parse(str);
        } catch (e) {
            return;
        };
    });
    /*---------格式化加工类型---------*/
    env.addFilter("formatType", (type) => {
        switch (type) {
            case "TP":
                return "贴牌";
            case "PF":
                return "批发";
            default:
                return type;
        }
    });
    /*----------格式化支付状态---------*/
    env.addFilter("formatPayStatus", (payStatus) => {
        switch (payStatus) {
            case true:
                return "已支付";
            case false:
                return "待支付";
        }
    });
    /*-----------格式化交易方式----------*/
    env.addFilter("formatTradeType", (tradeType) => {
        switch (tradeType) {
            case "ONLINE":
                return "在线担保交易";
            case "OFFLINE":
                return "线下担保交易";
        }
    });
    
    /*------------格式化-----------*/
    env.addFilter("productType", (product) => {
        let types = [];
        if (product.isTp) {
            types.push("贴牌");
        };
        if (product.isPf) {
            types.push("批发");
        };
        if (product.isDL) {
            types.push("一键代理");
        };
        return types.join("·");
    });
    env.addFilter("orderType", (type) => {
        switch (type) {
            case "TP":
                return "贴牌";
            case "PF":
                return "批发";
        };
    });

    /*--------格式化时间-----*/
    env.addFilter("formatDateTime", (timestamp, template) => {
        return moment(timestamp).format(template);
    });
    env.addFilter("getKey", (obj) => {
        let keys = _.keys(obj);
        return keys[0];
    });
    env.addFilter("getValue", (obj) => {
        let values = _.values(obj);
        return values[0];
    });
    env.addFilter("formatDiscount", (discount) => {
        return discount * 10 + "折";
    });

};